Page({
  data: {
    items:[
      {"good_id":"111","good_imageUrl":"http://www.cqcdj.com/upload/2017/1/12/636198179333129625.jpg",
        "good_name":"1","good_brand":"BMW",
        "good_selling_price":2332.1,"good_selling_count":23,
        "is_newest":true,"good_is_checked":false,"favorite_id":""
      },
      {"good_id":"222","good_imageUrl":"http://www.cqcdj.com/upload/2017/1/12/636198179333129625.jpg",
        "good_name":"2","good_brand":"BMW",
        "good_selling_price":2332.1,"good_selling_count":23,
        "is_newest":true,"good_is_checked":false,"favorite_id":""
      },
      
    ],
    isEdit:true,
    flag:true,
    button_txt:'编辑',
    bottem_flag:true,
    check_txt:'全选',
    favorite_ids : []
  },
  onLoad() {
    this.init();
  },
  init(){
    // get-favorite
          var img = this.items;

    var app = getApp()
      var url = app.globadData.apiUrlHeader+'get-favorite'
      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
                              "access_token":app.globadData.access_token
        }),
        dataType:'json',
        success: (res) => {
          console.log(res);
        if(res.data.status==0)
        {
          console.log(res);
           img = res.data.data;
          // console.log(img);
          this.setData({
           items : img
          });
        }
      },
    });
  },
  editTap(){
    console.log(this.data);
   if(this.data.isEdit){
    this.setData({
           button_txt : '完成',
           isEdit : false,
           flag : false,
           bottem_flag:false,
          });
   }else{
     this.setData({
           button_txt : '编辑',
           flag : true,
          isEdit : true,
          bottem_flag:true
          });
   }
  },

  onCheckboxGroupChange(e){
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var carts = this.data.items;
    
    var selected = carts[index].good_is_checked;
    carts[index].good_is_checked = !selected; 
    console.log('======');
    console.log(carts);
    console.log('======');
    //  this.setData({
    //        items : carts
    //     });
  },
  
  selectAll(e){
      var goods = this.data.items;
      for (var i = 0; i < goods.length; i++) {
        if(e.detail.value)
        goods[i].good_is_checked = true;
        else
        goods[i].good_is_checked = false;
      }
      this.setData({
           items : goods
        });
      // this.calTotal();
      console.log(this.data.items);
    },
    
    delTap(){
      var goods = this.data.items;
      
      // goods.forEach(function(good) {
      //   good = '';
      //   if(good.good_is_checked){
      //     good = '';
      //   }
      // }, this);

      console.log(goods);
      var count = 0;
      for (var i = 0; i < goods.length; i++) {
          if(goods[i].good_is_checked){
            //  this.favorite_ids += goods[i].favorite_id;
            // this.favorite_ids.push(goods[i].favorite_id);
         
        //调用删除收藏接口 del-favorite favorite_ids
        console.log('favorite_ids'+this.favorite_ids);
        var app = getApp()
      var url = app.globadData.apiUrlHeader+'del-favorite'
      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
          "favorite_ids":goods[i].favorite_id,
          "access_token":app.globadData.access_token
        }),
        dataType:'json',
        success: (res) => {
          console.log(res);
        if(res.data.status==0)
        {
          console.log(res);
           goods = res.data.data;
          // console.log(img);
          this.setData({
           items : goods
          });
        }
      },
    });
        
   }
        }
      // this.setData({
      //      items : goods
      //   });
    }


});
